package com.lancoo.dictionary.widget.globaltoast;

import com.lancoo.dictionary.widget.clipboard.ClipboardManagerCompat;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.text.TextUtils;

public final class ListenClipboardService extends Service implements GlobalToastController.ViewDismissHandler {

	private static final String KEY_FOR_WEAK_LOCK = "weak-lock";
	private static final String KEY_FOR_CMD = "cmd";
	private static final String KEY_FOR_CONTENT = "content";
	private static final String CMD_TEST = "test";

	private static CharSequence sLastContent = null;
	private ClipboardManagerCompat mClipboardWatcher;
	private GlobalToastController mTipViewController;
	private ClipboardManagerCompat.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener = new ClipboardManagerCompat.OnPrimaryClipChangedListener() {
		public void onPrimaryClipChanged() {
			performClipboardCheck();
		}
	};

	public static void start(Context context) {
		Intent serviceIntent = new Intent(context, ListenClipboardService.class);
		context.startService(serviceIntent);
	}

	/**
	 * for dev
	 */
	public static void startForTest(Context context, String content) {

		Intent serviceIntent = new Intent(context, ListenClipboardService.class);
		serviceIntent.putExtra(KEY_FOR_CMD, CMD_TEST);
		serviceIntent.putExtra(KEY_FOR_CONTENT, content);
		context.startService(serviceIntent);
	}

	public static void startForWeakLock(Context context, Intent intent) {

		Intent serviceIntent = new Intent(context, ListenClipboardService.class);
		context.startService(serviceIntent);

		intent.putExtra(ListenClipboardService.KEY_FOR_WEAK_LOCK, true);
		Intent myIntent = new Intent(context, ListenClipboardService.class);

		// using wake lock to start service
		WakefulBroadcastReceiver.startWakefulService(context, myIntent);
	}

	@Override
	public void onCreate() {
		mClipboardWatcher = ClipboardManagerCompat.create(this);
		mClipboardWatcher.addPrimaryClipChangedListener(mOnPrimaryClipChangedListener);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mClipboardWatcher.removePrimaryClipChangedListener(mOnPrimaryClipChangedListener);

		sLastContent = null;
		if (mTipViewController != null) {
			mTipViewController.setViewDismissHandler(null);
			mTipViewController = null;
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Utils.printIntent("onStartCommand", intent);

		if (intent != null) {
			// remove wake lock
			if (intent.getBooleanExtra(KEY_FOR_WEAK_LOCK, false)) {
				BootCompletedReceiver.completeWakefulIntent(intent);
			}
			String cmd = intent.getStringExtra(KEY_FOR_CMD);
			if (!TextUtils.isEmpty(cmd)) {
				if (cmd.equals(CMD_TEST)) {
					String content = intent.getStringExtra(KEY_FOR_CONTENT);
					showContent(content);
				}
			}
		}
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private void performClipboardCheck() {
		CharSequence content = mClipboardWatcher.getText();
		if (TextUtils.isEmpty(content)) {
			return;
		}
		showContent(content);
	}

	private void showContent(CharSequence content) {
		if (sLastContent != null && sLastContent.equals(content) || content == null) {
			return;
		}
		sLastContent = content;

		if (mTipViewController != null) {
			mTipViewController.updateContent(content);
		} else {
			mTipViewController = new GlobalToastController(getApplication(), content);
			mTipViewController.setViewDismissHandler(this);
			mTipViewController.show();
		}
	}

	@Override
	public void onViewDismiss() {
		sLastContent = null;
		mTipViewController = null;
	}

}